using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using ApiTest.Api.Entity;
using System.Linq;
using ApiTest.Api.Db;
using ApiTest.Api.Repository;
using ApiTest.Api.Params;
using Microsoft.Extensions.Configuration;
using ApiTest.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Admin3000.Backend.Api.Params;

namespace ApiTest.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class OrderController : ControllerBase
    {
        private IConfiguration _configuration;
        private IRepository<Order> _orderRepository;
        private TokenParameter _tokenParameter;

        public OrderController(IConfiguration configuration, IRepository<Order> orderRepository)
        {
            _configuration = configuration;
            _orderRepository = orderRepository;
            _tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();
        }


        [HttpGet("{id}")]
        public dynamic Get(int id)
        {
            var order=_orderRepository.GetById(id);
            var num=(order.Num*order.Price);

            return new{
                Code=1000,
                Data=new{order,num},
                Msg="查询订单成功"
            };
        }


        [HttpPost]
        public dynamic Post(CreateOrder neworder)
        {
            var order=new Order
            {
                Spuname=neworder.Spuname,
                UserId=neworder.UserId,
                Price=neworder.Price,
                Num=neworder.Num,
                State=neworder.State,
                Parameter=neworder.Parameter,
                ShopId=neworder.ShopId
            };
            _orderRepository.Insert(order);
            return new{
                Code=1000,
                Data=order,
                Msg="添加订单成功"
            };
        }

    }

}
